import click
from rich import get_console, reconfigure

reconfigure(log_path=False)
CON = get_console()


def _recursive_help(ctx: click.Context):
    cmd = ctx.command
    print(cmd.get_help(ctx))
    print()
    if isinstance(cmd, click.Group):
        for sub in cmd.commands.values():
            sub_ctx = sub.make_context(sub.name, [], ctx)
            _recursive_help(sub_ctx)


def help_all():
    def callback(ctx: click.Context, param: click.Parameter, value: bool):
        if not value or ctx.resilient_parsing:
            return
        _recursive_help(ctx)
        ctx.exit(1)

    return click.option(
        '--help-all',
        help='Show all help',
        is_flag=True,
        callback=callback,
        expose_value=False,
    )
